﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB.DAO;
using System.Web.Security;

namespace VNWIKI_WEB.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            User user = (User)Session["user"];
            if (user != null && user.Admin == true)
            {
                return View();
            }
            else
            {
               return RedirectToAction("LogOn", "Admin");
            }
        }

        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(string username, string password)
        {
            AccountDAO account = new AccountDAO();
            User user = account.Login(username.Trim(), password.Trim());
            if (user != null && user.Admin == true)
            {
                Session["user"] = user;
                Session["userName"] = user.AccountName;
                return RedirectToAction("Index", "Admin");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Account/LogOn

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            Session["user"] = null;
            Session["userName"] = null;
            return RedirectToAction("Index", "Admin");
        }

        public ActionResult UsersManager()
        {
            return RedirectToAction("Index", "User");
        }

        public ActionResult AlbumsManager()
        {
            return RedirectToAction("Index", "Album");

        }

        public ActionResult CategoriesManager()
        {
            return RedirectToAction("Index", "Category");
        }

        public ActionResult PhotosManager()
        {
            return RedirectToAction("Index", "Photo");
        }

        public ActionResult InsertAlbum(string albumName, int catID, string tags)
        {
            User user = (User)Session["user"];
            AdminDAO admin = new AdminDAO();
            int rs;
            rs = admin.InsertAlbum(albumName, tags, catID, user.ID);
            if (rs.Equals(1))
            {
                return View("PhotoUpload");
            }
            else
            {
                return View("AlbumCreate");
            }
        }

        public void InsertPhoto(string link, string index)
        {
            User user = (User)Session["user"];
            HomeDAO home = new HomeDAO();
            AdminDAO admin = new AdminDAO();
            Album album = home.GetAlbumNewInsert(user.ID);
            admin.InsertPhoto(album, index, link);
        }
    }
}
